<?

	class Config {
		
		private $file;
		private $config;
		
		public function __get($name) {
			if (key_exists($name, $this->config));return @$this->config[$name];
		}
		
		public function __set($n, $v) {
			$this->config[$n] = $v;
			return $v;
		}
		
		public function __construct($name) {
			$name = strtolower($name);
			$this->file = XTEN_DIR.'/config.php';
			if (file_exists($this->file)) include($this->file);
		}
		
		public function save() {
			$array = $this->config;
			$name = 'this->config';
			$ret = "<?\n";//global \$".preg_replace("(\[[^\]]*])", '', $name).";\n";
			$ret .= "\$$name = ".$this->r_print($array).";";
			$ret .= "?".">";
			$fp = @fopen($this->file, "w+");
			@fputs($fp, $ret);
			@fclose($fp);
			//file_put_contents($this->file, $ret);
			//if (!file_exists($this->file)) throw new Exception('can\'t write to "'.$file.'"');
			@chmod($this->file, 0777);
		}

		private function  r_print ($a) {
			$ret = "array(\n";
			foreach ($a as $key=>$val) {
				if ($val) {
					if (is_array($val)) $ret .= "\tstripslashes('".addslashes($key)."') => ".$this->r_print($val).", \n";
					else $ret .= "\tstripslashes('".addslashes($key)."')=>stripslashes('".addslashes($val)."'),\n";
				}
			}
			$ret .= ")";
			return $ret;
		}

	}
?>